<template>
  <view class="body">
    <view class="top_tip">
      <image class="img_bg" src="https://ainengli.hzjrsw.com/jkhx/healthBg.png" />
      <image class="img_robot" src="https://ainengli.hzjrsw.com/jkhx/robot.png" />
      <view class="top_content">
        <image class="img_tips" src="https://ainengli.hzjrsw.com/jkhx/tipBg.png" />
        <view class="switch" @tap="visitFun">
          <image class="img_switch" src="@/static/image/raozg/switch.png" />
          <text class="switch_text">就诊人</text>
        </view>
        <view class="tip_content">
          <view class="hello">{{ patient.patName }} 您好！</view>
          <text class="content">经AI评估您的数据完整度为:</text>
          <view class="percent">{{ integrity + '%' }}</view>
        </view>
      </view>
    </view>
    <view class="record">
      <view class="tips">
        <view class="tip_title">温馨提示</view>
        <text class="tip_content">
          根据目前您的结果，数据完整性不够，希望您进行信息补全，若未进行补全，画像评估的准确性降低...
        </text>
      </view>
      <view class="btns">
        <button class="ignore_btn" @tap="ignore">继续忽略，生成画像</button>
        <button class="fix_btn" @tap="fixData">补全数据</button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { getScaleInfo, saveScale } from '@/api/dataFix'
import { onShow, onLoad } from '@dcloudio/uni-app'
import { ref, computed } from 'vue'
import store from '@/store'
import { throttle, deepCopy } from '@/utils/tools'

let currentId = ''

const patient = computed(() => {
  return store.state.userInfo.patientInfo
})
// const ignore = () => {
//   uni.showToast({
//     title: '已忽略，生成画像'
//   })
// }
const fixData = throttle(() => {
  if (!store.state.dataFix.DATA.questions) {
    uni.showModal({
      title: '提示',
      content: '只能问答入口已关闭，点击确定返回健康画像',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          ignore()
        }
      }
    })
    return
  }
  uni.navigateTo({ url: '/packA/pages/dataFix/dataFix' })
}, 300)

const integrity = ref(0)

// watch(
//   () => store.state.userInfo.empi,
//   (id) => {
//     console.log(id)
//     getList()
//   }
// )

const getList = () => {
  // const id = store.state.userInfo.empi
  getScaleInfo(currentId).then((res) => {
    const DATA = res.data[0]

    if ((DATA.isFinish || DATA.integrity >= 80) && !notJump.value) {
      uni.reLaunch({ url: '/packA/pages/body/index' })
      return
    }
    integrity.value = DATA.integrity
    if (DATA.questions) {
      const d = JSON.parse(DATA.questions)
      d.map((v) => {
        if (v.options) {
          v.options = JSON.parse(v.options)
        }
        if (v.questionType.includes('多选')) {
          v.sort = []
        } else {
          v.sort = ''
        }
        if (v.showCondition) {
          v.showCondition = JSON.parse(v.showCondition)
        }
        if (v.rules) {
          v.rules = JSON.parse(v.rules)
        }
      })

      if (DATA.answers) {
        const answers = JSON.parse(DATA.answers)

        d.map((v) => {
          const item = answers.find((u) => {
            return u.questionCode === v.questionCode
          })

          if (item) {
            if (item.questionType === '输入') {
              v.chosen = item.chosen
            } else if (item.questionType.includes('单选')) {
              v.sort = item.sort
              const c = item.options.find((w) => {
                return w.sort === v.sort
              })
              v.chosen = c ? c.value : ''
            } else if (item.questionType.includes('多选')) {
              v.sort = item.sort
              v.chosen = item.options.reduce((m, n) => {
                if (item.sort.includes(n.sort)) {
                  if (m) {
                    return n.value
                  } else {
                    return m + '、' + n.value
                  }
                } else {
                  return m
                }
              }, '')
            }
          }
        })
      }
      DATA.questions = d
    }
    store.commit('dataFix/DATA_SET', DATA)
  })
}

const notJump = ref()

onLoad((query) => {
  if (query.notJump) {
    notJump.value = query.notJump
  } else {
    notJump.value = ''
  }
})

onShow(() => {
  if (!currentId || currentId !== store.state.userInfo.patientInfo.empi) {
    currentId = deepCopy(store.state.userInfo.patientInfo.empi)
    getList()
  }
})

const ignore = throttle(() => {
  const { empi, questions, scaleCode } = store.state.dataFix.DATA
  const params = {
    empi,
    scaleCode,
    // answers: JSON.stringify(questions),
    isFinish: true
  }
  uni.showLoading({
    title: '正在生成健康画像...'
  })

  saveScale(params)
    .then((res) => {
      uni.hideLoading()
      // uni.showToast({
      //   title: '已忽略，生成画像'
      // })
      uni.reLaunch({ url: '/packA/pages/body/index' })
    })
    .catch((err) => {
      uni.hideLoading()
    })
}, 300)
const visitFun = () => {
  uni.navigateTo({ url: '/packA/pages/family/index' })
}
</script>

<style lang="scss" scoped>
.body {
  width: 100%;
  height: 100vh;
  .top_tip {
    height: 262rpx;
    padding: 32rpx;
    display: flex;
    justify-content: flex-start;
    .img_bg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 342rpx;
    }
    .img_robot {
      width: 152rpx;
      height: 152rpx;
      position: relative;
      z-index: 2;
    }
    .top_content {
      border-radius: 16rpx;
      width: 522rpx;
      height: 192rpx;
      padding: 24rpx 34rpx;
      box-sizing: border-box;
      position: relative;
      .img_tips {
        position: absolute;
        top: 0;
        left: 0;
        width: 532rpx;
        height: 248rpx;
      }
      .switch {
        position: absolute;
        right: 0rpx;
        top: 0;
        z-index: 3;
        padding: 12rpx 16rpx;
        .img_switch {
          width: 24rpx;
          height: 24rpx;
        }
        .switch_text {
          font-weight: 500;
          font-size: 24rpx;
          color: #ffffff;
          margin-left: 6rpx;
        }
      }
      .tip_content {
        position: relative;
        z-index: 2;
      }
      .hello {
        font-weight: 500;
        font-size: 36rpx;
        color: #222222;
        margin-bottom: 8rpx;
      }
      .content {
        font-weight: 400;
        font-size: 30rpx;
        color: #222222;
        line-height: 50rpx;
      }
      .percent {
        font-family: DIN, DIN;
        font-weight: bold;
        font-size: 64rpx;
        color: #00d1b6;
      }
    }
  }
  .record {
    height: calc(100vh - 420rpx);
    margin-top: 48rpx;
    padding: 32rpx;
    padding-bottom: 64rpx;
    background: #fff;
    border-radius: 24rpx 24rpx 0 0;
    position: relative;
    z-index: 2;
    .tips {
      background: #fafafa;
      border-radius: 12rpx;
      padding: 32rpx;
      box-sizing: border-box;
      margin-bottom: 48rpx;
      .tip_title {
        width: 160rpx;
        background: #00d1b6;
        border-radius: 24rpx 0rpx 24rpx 0rpx;
        font-weight: 500;
        font-size: 28rpx;
        color: #ffffff;
        padding: 8rpx 24rpx;
        box-sizing: border-box;
        margin-bottom: 24rpx;
      }
      .tip_content {
        font-weight: 400;
        font-size: 30rpx;
        color: #222222;
        line-height: 54rpx;
      }
    }
    .btns {
      display: flex;
      justify-content: space-between;
      gap: 24rpx;
      .ignore_btn {
        width: 50%;
        height: 88rpx;
        line-height: 88rpx !important;
        background: #ffffff;
        border-radius: 44rpx;
        border: 1px solid #e5e6eb;
        font-weight: 400;
        font-size: 30rpx !important;
        padding: 0 !important;
        margin: 0;
        color: #666666;
      }
      .fix_btn {
        width: 50%;
        height: 88rpx;
        line-height: 88rpx !important;
        border-radius: 44rpx;
        background: #00d1b6;
        box-shadow: 0rpx 4rpx 16rpx 0rpx rgba(0, 159, 139, 0.34);
        font-weight: bold;
        font-size: 30rpx !important;
        padding: 0 !important;
        margin: 0;
        color: #ffffff;
      }
    }
  }
}
</style>
